package com.boxuegu.view;

import android.app.Activity;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.boxuegu.R;

public class BottomView {

    private View view;
    private LinearLayout title_bar;
    private FrameLayout main_body;
    private AppCompatActivity context;
    private CourseView courseView;
    private ExercisesView exercisesView;
    private MyInfoView myInfoView;
    private TitleBar titleBar;

    private View[] bodyViews=new View[3];
    private ImageView[] imageViews=new ImageView[3];
    private TextView[] textViews=new TextView[3];
    private int selected[]={R.drawable.main_course_icon_selected,R.drawable.main_exercises_icon_selected,R.drawable.main_my_icon_selected};
    private int unSelected[]={R.drawable.main_course_icon,R.drawable.main_exercises_icon,R.drawable.main_my_icon};

    private int currentSelected;//保存用户选中的选项。0：选中课程，1：习题，2：我

    public void setLoginStatus(String userName){
        myInfoView.setLoginStatus(userName);
    }

    private void switchPage(int newPage){
        switch (newPage){
            case 0:{//表示现在用户新选中的是课程
                //1、更换标题栏：如果是已经生成，则更换标题并显示，如果不存在则生成，并添加到主界面对应位置
                if(titleBar==null){
                    titleBar=new TitleBar(context,"博学谷课程");
                    title_bar.addView(titleBar.getView());
                }else {
                    titleBar.settitleName("博学谷课程");
                    titleBar.getView().setVisibility(View.VISIBLE);
                }
                break;
            }
            case 1:{
                if(titleBar==null){
                    titleBar=new TitleBar(context,"博学谷习题");
                    title_bar.addView(titleBar.getView());
                }else {
                    titleBar.settitleName("博学谷习题");
                    titleBar.getView().setVisibility(View.VISIBLE);
                }
                break;
            }
            case 2:{
                if(titleBar!=null){
                    titleBar.getView().setVisibility(View.GONE);
                }
                break;
            }
        }

        //2、更换中间的内容：前面选中的隐藏，新选中的，如果没有生成则，生成并添加到主界面中对应位置，否则显示
        if(bodyViews[currentSelected]!=null)
            bodyViews[currentSelected].setVisibility(View.GONE);

        if(bodyViews[newPage]==null){//当前新选中的中间页面还没有生成，则生成并添加到主界面中对应位置
            createBodyView(newPage);
        }else{
           bodyViews[newPage].setVisibility(View.VISIBLE);
        }

        //3、将底部原来的选中更换为未选中，将当前的底部按钮更换为选中
        imageViews[currentSelected].setImageResource(unSelected[currentSelected]);
        textViews[currentSelected].setTextColor(Color.parseColor("#666666"));
        imageViews[newPage].setImageResource(selected[newPage]);
        textViews[newPage].setTextColor(Color.parseColor("#0097f7"));

        currentSelected=newPage;
    }

    private void createBodyView(int newPage){
        switch (newPage){
            case 0:{
                courseView=new CourseView(context);
                main_body.addView(courseView.getView());
                bodyViews[0]=courseView.getView();
                break;
            }
            case 1:{
                exercisesView=new ExercisesView(context);
                main_body.addView(exercisesView.getView());
                bodyViews[1]=exercisesView.getView();
                break;
            }
            case 2:{
                myInfoView=new MyInfoView(context);
                main_body.addView(myInfoView.getView());
                bodyViews[2]=myInfoView.getView();
                break;
            }
        }
    }

    public BottomView(AppCompatActivity context){
        this.context=context;
        LayoutInflater layoutInflater=LayoutInflater.from(context);
        view=layoutInflater.inflate(R.layout.mian_view_bottom_bar,null);

        title_bar=context.findViewById(R.id.title_bar);
        main_body=context.findViewById(R.id.main_body);

        findBottomView();

        initDefault();

        imageViews[0].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //界面原来选中的选项在currentSelected里面保存
                //当用户选中课程按钮时，执行如下代码，主要有几个工作
                //1、更换标题栏：如果是课程或者习题，则更换标题文字，如果是我，则隐藏标题栏
                //2、更换中间的内容：前面选中的隐藏，新选中的，如果没有生成则，生成并添加到主界面中对应位置，否则显示
                //3、将底部原来的选中更换为未选中，将当前的底部按钮更换为选中
              switchPage(0);
            }
        });

        imageViews[1].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchPage(1);
            }
        });

        textViews[1].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchPage(1);
            }
        });

        imageViews[2].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchPage(2);
            }
        });

    }

    private void findBottomView(){
        imageViews[0]=view.findViewById(R.id.bottom_bar_image_course);
        imageViews[1]=view.findViewById(R.id.bottom_bar_image_exercises);
        imageViews[2]=view.findViewById(R.id.bottom_bar_image_myinfo);

        textViews[0]=view.findViewById(R.id.bottom_bar_text_course);
        textViews[1]=view.findViewById(R.id.bottom_bar_text_exercises);
        textViews[2]=view.findViewById(R.id.bottom_bar_text_myinfo);
    }

    private void initDefault(){
        currentSelected=0;
        //在主界面中添加标题栏和中间部分
        titleBar=new TitleBar(context,"博学谷课程");
        title_bar.addView(titleBar.getView());

        courseView=new CourseView(context);
        bodyViews[0]=courseView.getView();
        main_body.addView(courseView.getView());

        //设置底部对应项目为选中状态：就是更换图片，文字改色p80-81
        imageViews[0].setImageResource(selected[0]);
        textViews[0].setTextColor(Color.parseColor("#0097f7"));

    }


    public View getView() {
        return view;
    }
}
